// =============================================================================
// a simple program intended to show how easy it is to use libpappradio
// =============================================================================

#include "pappradio.hpp"

int main( int, char** )
{
    Pappradio pappradio;
    enum Pappradio::ERRORS result;

    // first load the fqdata into memory. Otherwise expect an error on open();

    pappradio.loadFQData( "fqdata.dat" );

    // -------------------------------------------------------------------------
    // try to find the pappradio on the bus ...
    // -------------------------------------------------------------------------
    //
    // NOTE:
    // this will either work as being root only or you'll have to configure
    // your udev-rules correctly. One possible solution is:
    //
    // SUBSYTEMS=="usb"
    //      ATTRS{idVendor}=="03eb"
    //      ATTRS{idProduct}=="2014"
    //      MODE:="0666"
    //      SYMLINK+="pappradio"
    //
    // This will then create "/dev/pappradio" whenever it is plugged in with
    // the normal user being able to use it.

    result = pappradio.open();

    if( result != Pappradio::ERR_NONE )
    {
        switch( result )
        {
            case Pappradio::ERR_INIT_FQDATA:
            {
                std::cerr << "*** ERROR: Loading fqdata.dat failed.\n" 
                          << std::endl;
                break;
            }
            case Pappradio::ERR_NONE:
            {
                std::cerr << "*** SUCCESS: Pappradio opened.\n" 
                          << std::endl;
                break;
            }
            default:
            {
                std::cerr << "*** ERROR: unknown error code.\n" 
                          << std::endl;
                break;
            }
        }
        return(0);
    }
    else
    {
        // OK, there is a pappradio...
        std::cerr << "Cool! A Pappradio was found!\n";
    }

    // Set correct bandpass-filter for 7.100 MHz
    pappradio.setFilter( Pappradio::BP_04000_07500 );

    // Set attenuator to -10 dB
    pappradio.setAttenuator( Pappradio::ATT00 );

    // Set desired LO-frequency to 7.100 MHz == 7100000 Hz
    pappradio.setFrequency( 7100000 );

    // check for actual LO-frequency
    std::cerr << "pappradio-LO running at "
              << pappradio.getFrequency()/1000000.0
              << " Mhz.\n";

    // Remark: depending on the fqdata.dat you use and it's content (eventual
    //         gaps, offset, range-defiencies... you may currently get a frequ
    //         ency quite far away from what you might want to get.
    //
    //         The actual LO-frequency nonetheless is correct, so, if you set
    //         7.1 MHz as desired frequency but in return get, say
    // close communication
    pappradio.close();

    return(0);
};

